#include <stdio.h>
#include <stdlib.h>
#include "myhash.h"

// 查询数字首次和最后出现的位置，不存在则返回0

// 使用myhash模块时，要去头文件中定义数据域
int main()
{
	int i, n = 6;
	int arr[6] = {2,3,3,1,2,2};

	// make hash
	HASH_INITIALIZE;

	hash_msg_t *ret;
	for (i = 0; i < n; i++) {
		hash_find(arr[i], &ret);
		if (ret == NULL) {
			hash_msg_t *temp = malloc(sizeof(*temp));
			temp->key = arr[i];
			temp->first = i;
			temp->last = i;
			hash_add(temp->key, temp);
		} else {
			ret->last = i;	//更新节点的last位置
		}
	}

	// search number
	int m = 4, ask[4] = {1,2,3,4};
	for (i = 0; i < m; i++) {
		hash_find(ask[i], &ret);
		if (ret == NULL) {
			printf("0\n");
		} else {
			printf("%d %d\n", ret->first, ret->last);
		}
	}

	// destory hash
	HASH_DESTROY;

	return 0;
}